﻿using System;
using ZFine.Application.Gas;
using ZFine.Application.SystemManage;
using ZFine.Code;
using ZFine.Domain.Entity.Gas;
using ZFine.Domain.Entity.SystemManage;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;

namespace ZFine.Web.Areas.Gas.Controllers
{
    public class SchoolController : ControllerBase
    {
        private SchoolApp schoolApp = new SchoolApp();
        private ClassApp classApp = new ClassApp();
        private RoleApp roleApp = new RoleApp();
        private UserApp userApp = new UserApp();
        private UserLogOnApp userLogOnApp = new UserLogOnApp();

        [HttpGet]
        [HandlerAuthorize(ignore: false)]
        public override ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(ignore: false)]
        public override ActionResult Form()
        {
            return View();
        }
        [HandlerAuthorize(ignore: false)]
        public override ActionResult Details()
        {
            return View();
        }
        [HandlerAuthorize(ignore: false)]
        public ActionResult Update()
        {
            return View();
        }
        [HandlerAuthorize(ignore: false)]
        public ActionResult Upload()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, SchoolEntity in_school,string SchoolType)
        {
            List<SchoolEntity> l_school = schoolApp.GetIQueryable(SchoolType).OrderBy(t=>t.SortId).ToList();
            var data = new
            {
                page = pagination.page,//当前页  1  2 3 之类
                records = pagination.records,//总条数  这里应该是18
                rows = l_school,//分页的数据
                total = pagination.total,//页数
            };
            return Content(data.ToJson());
        }
        //修改页数据
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJsonForUpdate(string keyValue)
        {
            SchoolEntity data = schoolApp.GetForm(keyValue);
            return Content(data.ToJson());
        }
        //详情页数据
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            SchoolEntity data = schoolApp.GetForm(keyValue);
            return Content(data.ToJson());
        }
        //取得最大的排序号
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMaxSortId()
        {
            int maxSortId = schoolApp.GetMaxSortId() + 1;
            return Content(maxSortId.ToJson());
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(SchoolEntity schoolEntity, string keyValue)
        {
            schoolApp.SubmitForm(schoolEntity, keyValue);
            this.registerSchool(schoolEntity);
            return Success("操作成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize(ignore: false)]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            schoolApp.Del(keyValue);
            return Success("删除成功。");
        }
        //取得学校列表
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSchoolSelectJson(string id)
        {
            var data = schoolApp.GetIQueryable(id).OrderBy(t => t.SortId).ToList();
            List<object> list = new List<object>();
            if (string.IsNullOrEmpty(id))
            {
                list.Add(new { id = "", text = "请选择学校" });
            }
            foreach (SchoolEntity item in data)
            {
                var tempNumber = item.SortId < 10 ? ("0" + item.SortId) : item.SortId.ToString();
                list.Add(new { id = item.F_Id, text = tempNumber + " " + item.SchoolName });
            }
            return Content(list.ToJson());
        }

        //注册学校信息
        public void registerSchool(SchoolEntity schoolEntity)
        {
            UserEntity userEntity = new UserEntity();
            UserLogOnEntity userLogOnEntity = new UserLogOnEntity();
            RoleEntity roleEntity = roleApp.GetRoleEntity("2");//学校注册（教育局添加）
            userEntity.F_Account = schoolEntity.SchoolAccount;
            userEntity.F_RoleId = roleEntity.F_Id;
            userEntity.F_IsAdministrator = false;
            userEntity.F_DeleteMark = false;
            userEntity.F_EnabledMark = true;
            userLogOnEntity.F_UserPassword = schoolEntity.SchoolPwd;
            var userList = userApp.GetByAccount(userEntity.F_Account).ToList();
            if (userList.Count() > 0)
            {
                //存在用户的话，只更新密码
                UserEntity dbuserEntity = userList[0];
                userLogOnApp.RevisePassword(schoolEntity.SchoolPwd, dbuserEntity.F_Id);
            }
            else
            {
                userApp.SubmitForm(userEntity, userLogOnEntity, "");
            }
        }
    }
}
